package speed2010.andreza.vspeed.util;

import java.io.BufferedReader;
import java.io.InputStreamReader;

/**
 * @author <a href="mailto:andreza.lba@gmail.com">Andreza Leite de Alencar</a>
 */
public final class ProcessUtils {

	private ProcessUtils() {
	}

	public static boolean isRunning(final String processName) throws Exception {
		boolean estaExecutando = false;

		final Runtime runtime = Runtime.getRuntime();
		final Process processo = runtime.exec("tasklist /FO CSV");

		final BufferedReader reader = new BufferedReader(new InputStreamReader(processo.getInputStream()));
		String linha = reader.readLine();
		while (linha != null) {
			linha = linha.replace("\"", "");
			final String[] partes = linha.split(",");
			if (partes[0].equalsIgnoreCase(processName)) {
				estaExecutando = true;
				break;
			}
			linha = reader.readLine();
		}
		reader.close();

		return estaExecutando;
	}

}
